package model

import (
	"gitee.com/enjoy_5/spikes-rpc/constant"
	"gitee.com/xuxinzhi2206/corehub/database/mysql"
	"gorm.io/gorm"
)

// 秒杀活动表
type SpikesActivity struct {
	gorm.Model
	SpikesTitle    string `gorm:"type:varchar(30);comment:活动标题;"`
	SpikeStartTime string `gorm:"type:datetime(0);comment:秒杀开始时间;"`
	SpikeEndTime   string `gorm:"type:datetime(0);comment:秒杀结束时间;"`
	SpikesStatus   int32  `gorm:"type:tinyint(1);default:0;comment:秒杀活动状态 0活动结束 1活动开启;"`
}

// 创建秒杀活动方法
func (s *SpikesActivity) CreateSpokes() (*SpikesActivity, error) {
	return s, mysql.WithMysql(func(db *gorm.DB) error {
		return db.Create(s).Error
	})
}

// 获取秒杀活动列表方法
func (s *SpikesActivity) GetSpokesActivityList() (list []*SpikesActivity, err error) {
	return list, mysql.WithMysql(func(db *gorm.DB) error {
		return db.Model(s).Find(&list).Error
	})
}

// 查询秒杀活动方法
func (s *SpikesActivity) GetSpokesBySpikesID() error {
	return mysql.WithMysql(func(db *gorm.DB) error {
		return db.Model(s).Where("id = ?", s.ID).First(s).Error
	})
}

// 秒杀活动开启方法
func (s *SpikesActivity) UpdateSpokesBySpikesID() error {
	return mysql.WithMysql(func(db *gorm.DB) error {
		return db.Model(s).Where("id = ?", s.ID).Update("spikes_status", constant.Open).Error
	})
}
